// main.js
const studentApp = createApp({
    data() {
        return {
            loading: false,
            resultTip:false,
            studentInfoModal:false,
            changePasswordModal:false,
            oldPassword: '',
            newPassword: '',
            error: '',
            student:[],
            grades: []
        }
    },
    methods: {
        // 加载学生信息
        async loadStudent(userNo) {
            try {
                const response = await fetch(window.contextPath+'/student/get?userNo=' + userNo);
                if (response.ok) {
                    this.student = await response.json();
                    await this.showResultTip()
                } else {
                    const errorText = await response.text();
                    this.error = '加载学生信息数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    await this.showError()
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                await this.showError()
            }
        },
        // 加载学生成绩信息
        async loadGrades(userNo) {
            this.loading = true;
            try {
                // 获取当前学生的所有成绩信息
                const response = await fetch(window.contextPath+'/grade/list?userNo=' + userNo);
                if (response.ok) {
                    this.grades = await response.json();
                    await this.showResultTip()
                } else {
                    const errorText = await response.text();
                    this.error = '加载数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    await this.showError()
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                await this.showError()
            } finally {
                this.loading = false;
            }
        },
        // 登出
        async logout() {
            alert('确定要退出吗')
            window.location.href = window.contextPath+'/api/logout';
        },
        // 显示结果提示模态框
        async showResultTip() {
            this.resultTip = true;
            // 显示几秒后关闭模态框
            setTimeout(() => {
                this.resultTip = false;
            }, 3000)
        },
        // 显示错误提示模态框
        async showError() {
            // 显示几秒后关闭模态框
            setTimeout(() => {
                this.error = '';
            }, 3000)
        },
        async showStudentInfo() {
            this.loadStudent(window.userNo).then(r => console.log('加载学生信息成功'))
            this.studentInfoModal = true;
        },
        async hideStudentInfo() {
            this.studentInfoModal = false;
        },
        async showChangePassword() {
            this.oldPassword = '';
            this.newPassword = '';
            this.changePasswordModal = true;
        },
        async hideChangePassword() {
            this.changePasswordModal = false;
        },
        async changePassword(userNo) {
            // 密码强度验证
            if (this.oldPassword === '' || this.newPassword === '') {
                this.error = '请填写旧密码和新密码';
                await this.showError();
                return;
            } else if (this.oldPassword === this.newPassword) {
                this.error = '新密码不能与旧密码相同';
                await this.showError();
                return;
            } else if (this.newPassword.length < 6) {
                this.error = '新密码长度不能小于6位';
                await this.showError();
                return;
            } else if (this.newPassword.length > 20) {
                this.error = '新密码长度不能大于20位';
                await this.showError();
                return;
            } else if (!/^(?=.*[a-zA-Z])(?=.*[0-9])/.test(this.newPassword)) {
                this.error = '新密码必须包含字母和数字';
                await this.showError();
                return;
            }

            try {
                const response = await fetch(window.contextPath+'/api/changePassword', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        userNo: userNo,
                        oldPassword: this.oldPassword,
                        newPassword: this.newPassword
                    })
                });

                if (response.ok) {
                    await this.showResultTip()
                    alert('修改密码成功，请重新登录')
                    window.location.href = window.contextPath+'/api/logout';
                } else {
                    const errorText = await response.text();
                    this.error = '修改密码失败,错误码:'+ response.status +',错误信息: ' + errorText
                    await this.showError()
                }
            }catch (error) {
                this.error = '网络错误: ' + error.message;
                await this.showError();
            }
        }

    },
    mounted() {
        window.studentAppInstance = this;
        this.loadGrades(window.userNo).then(r => console.log('加载成绩信息成功'))
    },

});
studentApp.mount('#student-app');

